package com.example.charge

import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.charge.adapter.ChargeAdapter
import com.example.charge.databinding.ActivityChargeListBinding
import com.example.charge.vm.ChargeViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/charge/ChargeListActivity")
@AndroidEntryPoint
class ChargeListActivity : BaseMvvmActivity<ActivityChargeListBinding>() {
    override fun getLayoutId(): Int = R.layout.activity_charge_list
    val chargeAdapter = ChargeAdapter()
    var payerId = 1
    private val chargeViewModel : ChargeViewModel by viewModels()
    override fun initView() {
        super.initView()
        initSpinners()
        initButtons()

        mDataBinding.back.setOnClickListener {
            finish()
        }
        mDataBinding.rv.adapter = chargeAdapter
        mDataBinding.rv.layoutManager = LinearLayoutManager(this)

        chargeViewModel.getChargeList(payerId)
        chargeViewModel.mSuccessLiveData.observe(this){
               ToastUtils.showLong("获取成功")
            chargeAdapter.setList(it.data)
            chargeAdapter.notifyDataSetChanged()
        }
        chargeViewModel.mErrorLiveData.observe(this){
                   ToastUtils.showLong(it)
        }
        chargeAdapter.setOnItemClickListener { adapter, view, position ->
                        val chargeEntity = chargeAdapter.getItem(position)
                        ARouter.getInstance().build("/charge/ChargeDetailActivity")
                            .withParcelable("chargeEntity",chargeEntity)
                            .navigation()
        }
    }
        override fun onResume() {
            super.onResume()
            chargeViewModel.getChargeList(payerId)
        }

    private fun initButtons() {
        // 设置本月按钮为选中状态
        updateButtonStyle(true)

        // 本月按钮点击事件
        mDataBinding.btnMonth.setOnClickListener {
            updateButtonStyle(true)
        }

        // 本年按钮点击事件
        mDataBinding.btnYear.setOnClickListener {
            updateButtonStyle(false)
        }
    }

    /**
     * 更新按钮样式
     * @param isMonthSelected true表示选中本月，false表示选中本年
     */
    private fun updateButtonStyle(isMonthSelected: Boolean) {
        if (isMonthSelected) {
            // 本月按钮选中样式
            mDataBinding.btnMonth.setBackgroundResource(R.drawable.btn_selected_background)
            mDataBinding.btnMonth.setTextColor(resources.getColor(android.R.color.white))

            // 本年按钮未选中样式
            mDataBinding.btnYear.setBackgroundResource(R.drawable.btn_unselected_background)
            mDataBinding.btnYear.setTextColor(resources.getColor(android.R.color.black))
        } else {
            // 本年按钮选中样式
            mDataBinding.btnYear.setBackgroundResource(R.drawable.btn_selected_background)
            mDataBinding.btnYear.setTextColor(resources.getColor(android.R.color.white))

            // 本月按钮未选中样式
            mDataBinding.btnMonth.setBackgroundResource(R.drawable.btn_unselected_background)
            mDataBinding.btnMonth.setTextColor(resources.getColor(android.R.color.black))
        }
    }
    // 初始化筛选下拉框
    private fun initSpinners() {
        // 项目筛选数据
        val projectList = listOf("项目", "水电费", "物业费", "宿舍费")
        val projectAdapter =
            object : ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projectList) {
                override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                    val view = super.getView(position, convertView, parent)
                    (view as TextView).setTextSize(12f) // 设置字体大小
                    return view
                }

                override fun getDropDownView(
                    position: Int,
                    convertView: View?,
                    parent: ViewGroup
                ): View {
                    val view = super.getDropDownView(position, convertView, parent)
                    (view as TextView).setTextSize(12f)  // 设置下拉列表字体大小
                    return view
                }
            }
        projectAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.spinnerProject.adapter = projectAdapter

        // 状态筛选数据
        val statusList = listOf("状态", "待缴费", "已缴费", "欠费")
        val statusAdapter =
            object : ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, statusList) {
                override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                    val view = super.getView(position, convertView, parent)
                    (view as TextView).setTextSize(12f)
                    return view
                }

                override fun getDropDownView(
                    position: Int,
                    convertView: View?,
                    parent: ViewGroup
                ): View {
                    val view = super.getDropDownView(position, convertView, parent)
                    (view as TextView).setTextSize(12f)
                    return view
                }
            }
        statusAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.spinnerStatus.adapter = statusAdapter

        // 账单数量数据
        val billCountList = listOf("账单", "1-10条", "10-50条", "50条以上")
        val billCountAdapter = object :
            ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, billCountList) {
            override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                val view = super.getView(position, convertView, parent)
                (view as TextView).setTextSize(12f)
                return view
            }

            override fun getDropDownView(
                position: Int,
                convertView: View?,
                parent: ViewGroup
            ): View {
                val view = super.getDropDownView(position, convertView, parent)
                (view as TextView).setTextSize(12f)
                return view
            }
        }
        billCountAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        mDataBinding.spinnerBillCount.adapter = billCountAdapter

    }

}